package community.hospital.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;

import java.nio.charset.StandardCharsets;

/**
 * @Author：ydj
 * @Date：2023/1/17 14:50
 */
public class RandomUtils {
    private static final String KEY = "1111111111111111";
    public static String getSalt() {
        byte[] bytes = RandomUtil.randomBytes(6);
        return new String(bytes);
    }

    public static String getToken(Long userId) {
        byte[] encrypt = SecureUtil.aes(KEY.getBytes(StandardCharsets.UTF_8)).encrypt(userId.toString());
        String token = HexUtil.encodeHexStr(encrypt);
        return token;
    }

    public static Long parseToken(String token) {
        byte[] bytes = HexUtil.decodeHex(token);
        String decrypt = SecureUtil.aes(KEY.getBytes(StandardCharsets.UTF_8)).decryptStr(bytes);
        return Long.valueOf(decrypt);
    }

    public static void main(String[] args) {

    }

}
